﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;
using cart.utils;
using cart.model;

namespace cart.dao
{
    public class productDao
    {

        /// <summary>
        /// 根据类型得到商品
        /// 2009年12月15日19:26:19 ousinka
        /// </summary>
        /// <param name="typeName">类型名称</param>
        /// <param name="topNumber">取前几个</param>
        /// <returns></returns>
        public ArrayList getProductByType(string typeName, int topNumber)
        {
            string getProductByTypeSQL = "SELECT top (@num) *  FROM PRODUCT";
            SqlConnection conn = DB.getSqlConnection();
            DB.connectionOpen(conn);
            SqlCommand cmd = new SqlCommand(getProductByTypeSQL + " where " + typeName + " = 1 order by AddTime desc", conn);
            cmd.Parameters.AddWithValue("@num", topNumber);
            SqlDataReader sdr = cmd.ExecuteReader();
            ArrayList productList = new ArrayList();
            while (sdr.Read())
            {
                ProductModel p = new ProductModel();
                p.productID = sdr.GetInt32(0);
                p.name = sdr.GetString(2);
                p.productNo = sdr.GetString(3);
                p.brand = sdr.GetString(4);
                p.marketPrice = Convert.ToDouble(sdr.GetValue(8));
                p.memberPrice = Convert.ToDouble(sdr.GetValue(9));
                p.thumbnailImg = sdr.GetString(11);
                p.productImg = sdr.GetString(12);
                p.addTime = sdr.GetDateTime(21);
                productList.Add(p);
            }
            DB.connectionClose(conn);
            return productList;
        }

        public ProductModel getProductByID(int productID)
        {
            SqlConnection conn = DB.getSqlConnection();
            DB.connectionOpen(conn);
            SqlCommand cmd = new SqlCommand("select * from product where productID = " + productID, conn);
            SqlDataReader sdr = cmd.ExecuteReader();
            sdr.Read();

            ProductModel pm = new ProductModel();
            pm.productID = sdr.GetInt32(0);
            pm.name = sdr.GetString(2);
            pm.marketPrice = Convert.ToDouble(sdr.GetValue(8));
            pm.memberPrice = Convert.ToDouble(sdr.GetValue(9));
            pm.score = sdr.GetInt32(10);
            DB.connectionClose(conn);
            return pm;
        }
    }
}